package Hash;

import java.awt.RenderingHints.Key;
import java.util.Scanner;

import org.omg.PortableServer.ID_ASSIGNMENT_POLICY_ID;

public class HashTabDemo {
	// 谷歌问题：添加公司员工的个人信息（id、姓名），并且，能通过id查询该员工的信息。
	// 要求：不使用数据库、速度越快越好、 =》哈希表（散列）
	// 思路：建立三个类：员工信息节点类、单链表类、哈希表类

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("请输入待创建哈希表的长度：");
		Scanner scanner = new Scanner(System.in);
		int size = scanner.nextInt();
		String key;
		int id;
		String name;
		HashTab hashTab = new HashTab(size);
		System.out.println("创建成功！");

		while (true) {
			System.out.println("add:添加");
			System.out.println("delete：删除");
			System.out.println("list：展示所有节点信息");
			System.out.println("show：展示单个节点信息");
			System.out.println("update：更新单个节点信息");
			System.out.println("exit：退出");
			key = scanner.next();
			switch (key) {
			case "add":
				System.out.println("请输入节点id：");
				id = scanner.nextInt();
				System.out.println("请输入节点name：");
				name = scanner.next();
				hashTab.add(id, name);
				break;
			case "delete":
				System.out.println("请输入待删除节点id：");
				id = scanner.nextInt();
				hashTab.delete(id);
				break;
			case "list":		
				hashTab.list();
				break;
			case "show":
				System.out.println("请输入节点id：");
				id = scanner.nextInt();
				hashTab.show(id);
				break;
			case "update":
				System.out.println("请输入待更新节点id：");
				id = scanner.nextInt();
				System.out.println("请输入待更新节点name：");
				name = scanner.next();
				hashTab.update(id, name);
				break;
			case "exit":
				scanner.close();
				System.exit(0);				
			default:
				break;

			}
		}

	}

}

//建立员工节点信息类
class Node {
	private int id;
	private String name;
	private Node next;
	
	

	public Node(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Node getNext() {
		return next;
	}

	public void setNext(Node next) {
		this.next = next;
	}

	@Override
	public String toString() {
		return "Node [id=" + id + ", name=" + name + "]";
	}
}

//建立单链表类
class InfoLinkedList {
	private Node headNode;
	private Node tempNode;
	// 添加节点
	public void add(Node node) {
		if (headNode == null) {
			headNode = node;
			return;
		}
		tempNode = headNode;
		while (tempNode.getNext() != null) {
			tempNode = tempNode.getNext();
		}
		tempNode.setNext(node);
	}

	// 删除
	public void delete(int id) {
		if (headNode == null) {
			System.out.println("链表为空，不能删除！");
			return;
		}
		tempNode = headNode;
		if (tempNode.getId() == id) {
			headNode = tempNode.getNext();
			return;
		}
		while (tempNode.getNext() != null) {
			if (tempNode.getNext().getId() == id) {
				tempNode.setNext(tempNode.getNext().getNext());
				return;
			} else {
				tempNode = tempNode.getNext();
			}
		}
		System.out.println("未找到id为" + id + "的节点，删除失败！");
		return;
	}

	// 展示全部节点
	public void list(int id) {
		if (headNode == null) {
			System.out.println("第" + (id + 1) + "个链表为空，没有信息！");
			return;
		}
		System.out.print("第" + (id + 1) + "个链表：");
		tempNode = headNode;
		while (tempNode != null) {
			System.out.printf("id==>%d,name==>%s    ", tempNode.getId(), tempNode.getName());
			tempNode = tempNode.getNext();
		}
		System.out.println();
	}

	// 查找节点
	public Node show(int id) {
		if (headNode == null) {
			System.out.println("列表为空，查找失败！");
			return null;
		}
		tempNode = headNode;

		while (tempNode != null) {
			if (tempNode.getId() == id) {
				return tempNode;
			}
			tempNode = tempNode.getNext();
		}
		System.out.println("未找到id为" + id + "的节点，查找失败！");
		return null;
	}

	// 修改单个节点
	public void update(Node node) {
		if (headNode == null) {
			System.out.println("列表为空，修改失败！");
			return;
		}
		tempNode = headNode;
		while (tempNode != null) {
			if (tempNode.getId() == node.getId()) {
				tempNode.setName(node.getName());
				return;
			}
			tempNode = tempNode.getNext();
		}
		System.out.println("未找到id为" + node.getId() + "的节点，修改失败！");

	}

}

//建立哈希表
class HashTab {
	private int size;
	private InfoLinkedList[] infoLinkedListArray;
	private int linkedListId;
	

	public HashTab(int size) {
		this.size = size;
		infoLinkedListArray = new InfoLinkedList[size];
		for (int i = 0; i < size; i++) {
			infoLinkedListArray[i] = new InfoLinkedList();
		}
	}

	// 通过id，获的单链表的id
	public int getLinkedListId(int id) {
		return id % size;
	}

	// 添加节点
	public void add(int id, String name) {
		Node node = new Node(id,name);
		linkedListId = getLinkedListId(node.getId());
		infoLinkedListArray[linkedListId].add(node);
	}

	// 删除节点
	public void delete(int id) {
		linkedListId = getLinkedListId(id);
		infoLinkedListArray[linkedListId].delete(id);
	}

	// 查看所有节点信息
	public void list() {
		for (int i = 0; i < size; i++) {
			infoLinkedListArray[i].list(i);

		}
	}

	// 查找单个节点
	public Node show(int id) {
		linkedListId = getLinkedListId(id);
		Node node = infoLinkedListArray[linkedListId].show(id);
		System.out.println(node.toString());
		return node;
	}

	// 修改单个节点
	public void update(int id, String name) {
		Node node = new Node(id,name);
		linkedListId = getLinkedListId(node.getId());
		infoLinkedListArray[linkedListId].update(node);
	}

}
